package com.vanzo.instruction;


import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by tisen on 2018/3/1.
 */

public class TalkiePower {
    public static final String TAG = "TalkiePower";
    private static final String TALKIE_STATE_PATH = "sys/devices/platform/talkie@0/talkieenablestatus";
    public static final String TALKIE_STATE_OFF = "0";
    public static final String TALKIE_STATE_ON = "1";

    private static void setTalkieStatus(String path, String node) {
        try {
            BufferedWriter bufWriter = null;
            bufWriter = new BufferedWriter(new FileWriter(path));
            bufWriter.write(node);  // 写操作
            bufWriter.close();
            Log.d(TAG, " write successful talkie power = " + node + " by IO");
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, " can't write " + path + e.getMessage(), e);
        }
    }

    private static String getTalkieStatus(String path) {
        String prop = "null";
        try {
            BufferedReader reader = new BufferedReader(new FileReader(path));
            prop = reader.readLine();
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return prop;
    }

    public static void on() {
        setTalkieStatus(TALKIE_STATE_PATH, TALKIE_STATE_ON);
    }

    public static void off() {
        setTalkieStatus(TALKIE_STATE_PATH, TALKIE_STATE_OFF);
    }

    public static boolean status() {
        return getTalkieStatus(TALKIE_STATE_PATH).equals(TALKIE_STATE_ON);
    }
}
